<?php
class Helper_BSS_Array
{
	/**
	 * 搜索二维数组
	 * @param $haystack 传入数组
	 * @param $key 第二层数组key
	 * @param $value 第二层数组value
	 * @return 找到的第二层数组
	 */
	static function Search($haystack, $key, $value)
	{
		$ret = array();
		foreach ($haystack as $h)
		{
			if ($h[$key] == $value)
			{
				$ret[] = $h;
			}
		}
		return $ret;
	}
	
	static function getTree($items)
	{
		 foreach ($items as $item)
	        $items[$item['pid']]['children'][$item['id']] = &$items[$item['id']];
	    return isset($items[0]['children']) ? $items[0]['children'] : array();
	}
	
	static function SortByValue($arr, $key, $sort='asc')
	{
		$new_key = array();
		foreach ($arr as $k => $a)
		{
			$new_key[$k] = $a[$key];
		}
		if ($sort == 'asc')
		{
			asort($new_key);
		}
		if ($sort == 'desc')
		{
			arsort($new_key);
		}
		$ret = array();
		foreach ($new_key as $k => $n)
		{
			$ret[] = $arr[$k];
		}
		return $ret;
	}

	/**
	 * 获取捆绑item表产品的数据
	 * @param $post 提交的item数据
	 */
	static function getProductBindItemToArray($post)
	{
		$key_arr = array();
		$item_arr =array();
		foreach ($post as $key => $value)
		{
			if (substr($key,0,11) == 'product_id_')
			{
				$key_arr[] = substr($key,11,strlen($key)-11);
			}
		}
		foreach ($key_arr as $k)
		{
			$temp = array();
			$temp['product_basic_id'] = $post['product_id_' . $k];
			$temp['quantity'] = intval($post['product_quantity_' . $k]);
			$item_arr[$k] = $temp;
		}
		return $item_arr;
	}
	
	/*
	* 在二维数组获取键值为$key的元素，并组装成新的数组
	* $param $array 二维数组
	* $param $key 需要从二维数组中获取元素的键值
	*/
	static function getDistinctValueToArrayByKey($array, $key)
	{
		$new = array();
		foreach ($array as $a)
		{
			$new[] = $a[$key];
		}
		return array_unique($new);
	}
	
	static function EbayStoreCategoriesArray($categories)
	{
		$ret = array();
		foreach ($categories as $c)
		{
			$ret[] = array('value' => $c->CategoryID, 'name' => $c->Name);
			if (isset($c->ChildCategory))
			{
				if (is_array($c->ChildCategory))
				{
					$child_categories = $c->ChildCategory;
				}
				else
				{
					$child_categories = array($c->ChildCategory);
				}
				$c_categories = Helper_BSS_Array::EbayStoreCategoriesArray($child_categories);
				foreach ($c_categories as $cc)
				{
					$ret[] = array('value' => $cc['value'], 'name' => $c->Name . ' > ' . $cc['name']);
				}
			}
		}
		return $ret;
	}
	
	static function EbayDetailArray($detail)
	{
		$ret = array();
		//Dispatch
		if (is_array($detail->DispatchTimeMaxDetails))
		{
			$dispatch = $detail->DispatchTimeMaxDetails;
		}
		else
		{
			$dispatch = array($detail->DispatchTimeMaxDetails);
		}
		foreach ($dispatch as $d)
		{
			$ret['dispatch'][$d->DispatchTimeMax] = array('name' => $d->Description, 'value' => $d->DispatchTimeMax);
		}
		ksort($ret['dispatch']);
		//Shipping Service
		if (is_array($detail->ShippingServiceDetails))
		{
			$shipping_service = $detail->ShippingServiceDetails;
		}
		else
		{
			$shipping_service = array($detail->ShippingServiceDetails);
		}
		$domestic = $international = array();
		foreach ($shipping_service as $ss)
		{
			if (isset($ss->ValidForSellingFlow) && $ss->ValidForSellingFlow && isset($ss->ServiceType) && isset($ss->ShippingCategory))
			{
				if ($ss->ShippingServiceID <= 50000)
				{
					$domestic[$ss->ShippingCategory][] = array('name' => $ss->Description, 'value' => $ss->ShippingService);
				}
				else
				{
					$international[$ss->ShippingCategory][] = array('name' => $ss->Description, 'value' => $ss->ShippingService);
				}
			}
		}
		$domestic_shipping_service_option_html = $international_shipping_service_option_html = '<option value="">--none--</option>';
		foreach ($domestic as $category => $d)
		{
			$domestic_shipping_service_option_html .= '<optgroup label="' . $category . '">';
			foreach ($d as $v)
			{
				$domestic_shipping_service_option_html .= '<option value="' . $v['value'] . '">' . addslashes($v['name']) . '</option>';
			}
			$domestic_shipping_service_option_html .= '</optgroup>';
		}
		foreach ($international as $category => $i)
		{
			$international_shipping_service_option_html .= '<optgroup label="' . $category . '">';
			foreach ($i as $v)
			{
				$international_shipping_service_option_html .= '<option value="' . $v['value'] . '">' . addslashes($v['name']) . '</option>';
			}
			$international_shipping_service_option_html .= '</optgroup>';
		}
		$ret['shipping_service'] = array('domestic' => $domestic, 'international' => $international, 'domestic_shipping_service_option_html' => $domestic_shipping_service_option_html, 'international_shipping_service_option_html' => $international_shipping_service_option_html);
		
		//shipping location
		if (is_array($detail->ShippingLocationDetails))
		{
			$location = $detail->ShippingLocationDetails;
		}
		else
		{
			$location = array($detail->ShippingLocationDetails);
		}
		foreach ($location as $l)
		{
			if ( ! in_array($l->ShippingLocation, array('None', 'Worldwide')))
			{
				$ret['shipping_location'][] = array('name' => $l->Description, 'value' => $l->ShippingLocation);
			}
		}
		//return policy
		if (isset($detail->ReturnPolicyDetails))
		{
			if (isset($detail->ReturnPolicyDetails->ReturnsWithin))
			{
				if (is_array($detail->ReturnPolicyDetails->ReturnsWithin))
				{
					$return_within = $detail->ReturnPolicyDetails->ReturnsWithin;
				}
				else
				{
					$return_within = array($detail->ReturnPolicyDetails->ReturnsWithin);
				}
				foreach ($return_within as $rw)
				{
					$ret['return_policy']['return_within'][] = array('name' => $rw->Description, 'value' => $rw->ReturnsWithinOption);
				}
			}
			if (isset($detail->ReturnPolicyDetails->ReturnsAccepted))
			{
				if (is_array($detail->ReturnPolicyDetails->ReturnsAccepted))
				{
					$return_accepted = $detail->ReturnPolicyDetails->ReturnsAccepted;
				}
				else
				{
					$return_accepted = array($detail->ReturnPolicyDetails->ReturnsAccepted);
				}
				foreach ($return_accepted as $ra)
				{
					$ret['return_policy']['return_accepted'][] = array('name' => $ra->Description, 'value' => $ra->ReturnsAcceptedOption);
				}
			}
			if (isset($detail->ReturnPolicyDetails->Description))
			{
				$ret['return_policy']['description'][] = array('name' => 'description', 'value' => $detail->ReturnPolicyDetails->Description);
			}
			else
			{
				$ret['return_policy']['description'][] = array('name' => 'description', 'value' => false);
			}
			if (isset($detail->ReturnPolicyDetails->ShippingCostPaidBy))
			{
				if (is_array($detail->ReturnPolicyDetails->ShippingCostPaidBy))
				{
					$shipping_cost_paid_by = $detail->ReturnPolicyDetails->ShippingCostPaidBy;
				}
				else
				{
					$shipping_cost_paid_by = array($detail->ReturnPolicyDetails->ShippingCostPaidBy);
				}
				foreach ($shipping_cost_paid_by as $scpb)
				{
					$ret['return_policy']['shipping_cost_paid_by'][] = array('name' => $scpb->Description, 'value' => $scpb->ShippingCostPaidByOption);
				}
			}
			if (isset($detail->ReturnPolicyDetails->Refund))
			{
				if (is_array($detail->ReturnPolicyDetails->Refund))
				{
					$refund = $detail->ReturnPolicyDetails->Refund;
				}
				else
				{
					$refund = array($detail->ReturnPolicyDetails->Refund);
				}
				foreach ($refund as $r)
				{
					$ret['return_policy']['refund'][] = array('name' => $r->Description, 'value' => $r->RefundOption);
				}
			}
			if (isset($detail->ReturnPolicyDetails->RestockingFeeValue))
			{
				if (is_array($detail->ReturnPolicyDetails->RestockingFeeValue))
				{
					$restocking_fee = $detail->ReturnPolicyDetails->RestockingFeeValue;
				}
				else
				{
					$restocking_fee = array($detail->ReturnPolicyDetails->RestockingFeeValue);
				}
				foreach ($restocking_fee as $rf)
				{
					$ret['return_policy']['restocking_fee'][] = array('name' => $rf->Description, 'value' => $rf->RestockingFeeValueOption);
				}
			}
		}
		return $ret;
	}
	
	static function EbayCategoryFeaturesArray($features)
	{
		$ret = array();
		if (isset($features->VariationsEnabled))
		{
			$ret['variation_enabled'] = true;
		}
		else
		{
			$ret['variation_enabled'] = false;
		}
		if (is_array($features->ConditionValues->Condition))
		{
			$condition = $features->ConditionValues->Condition;
		}
		else
		{
			$condition = array($features->ConditionValues->Condition);
		}
		foreach ($condition as $c)
		{
			$ret['condition'][] = array('id' => $c->ID, 'display_name' => $c->DisplayName);
		}
		return $ret;
	}
	
	static function EbayCategorySpecificsArray($specifics)
	{
		if (is_array($specifics->NameRecommendation))
		{
			$specifics = $specifics->NameRecommendation;
		}
		else
		{
			$specifics = array($specifics->NameRecommendation);
		}
		$ret = array();
		foreach ($specifics as $s)
		{
			$recommendation = array();
			if (isset($s->ValueRecommendation))
			{
				if (is_array($s->ValueRecommendation))
				{
					$value = $s->ValueRecommendation;
				}
				else
				{
					$value = array($s->ValueRecommendation);
				}
				foreach ($value as $v)
				{
					$recommendation[] = htmlspecialchars($v->Value);
				}
			}
			$ret[] = array(
				'name' => $s->Name,
				'min_value' => (isset($s->ValidationRules->MinValues) ? $s->ValidationRules->MinValues : 0),
				'mode' => $s->ValidationRules->SelectionMode,
				'variation' => (isset($s->ValidationRules->VariationSpecifics) && $s->ValidationRules->VariationSpecifics == 'Disabled') ? 0 : 1,
				'recommendation' => $recommendation
			);
		}
		return $ret;
	}
	
	static function EbayItemsArray($item, $shop)
	{
		$listings = array();
		foreach ($item as $i)
		{
			$one = array();
			$one['shop_id'] = $shop['id'];
			$one['site'] = $i->Site;
			$one['currency_code'] = $i->Currency;
			$one['payment_method'] = 'PayPal';
			$one['payment_address'] = $shop['payment_address'];
			$one['item_id'] = $i->ItemID;
			$one['primary_category'] = $i->PrimaryCategory->CategoryID;
			$one['title'] = $i->Title;
			$one['listing_type'] = $i->ListingType;
			$one['listing_duration'] = $i->ListingDuration;
			$one['counter'] = $i->HitCounter;
			$one['hit_count'] = isset($i->HitCount) ? $i->HitCount : 0;
			$one['sell_status'] = Helper_BSS_Normal::getIniOne('online_ebay_listing_sell_status', 'name', $i->SellingStatus->ListingStatus, 'value');
			$one['start_time'] = Helper_BSS_Normal::timeStringToStandard($i->ListingDetails->StartTime);
			$one['end_time'] = Helper_BSS_Normal::timeStringToStandard($i->ListingDetails->EndTime);
			$one['view_url'] = $i->ListingDetails->ViewItemURL;
			$one['watch_count'] = $i->WatchCount;
			$one['quantity'] = $i->Quantity - $i->SellingStatus->QuantitySold;
			$one['sold_quantity'] = $i->SellingStatus->QuantitySold;
			$one['start_price'] = $i->StartPrice;
			$one['location'] = $i->Location;
			$picture = array();
			if (isset($i->PictureDetails->PictureURL))
			{
				if (is_array($i->PictureDetails->PictureURL))
				{
					$picture = $i->PictureDetails->PictureURL;
				}
				else
				{
					$picture = array($i->PictureDetails->PictureURL);
				}
			}
			if (isset($i->PictureDetails->GalleryURL))
			{
				$index = array_search($i->PictureDetails->GalleryURL, $picture);
				if ($index !== false)
				{
					unset($picture[$index]);
				}
				array_unshift($picture, $i->PictureDetails->GalleryURL);
			}
			$one['picture']['listing_picture'] = array();
			foreach ($picture as $k => $p)
			{
				$one['picture']['listing_picture'][] = array('specific_name' => '', 'specific_value' => '', 'picture_url' => $p, 'sort' => $k);
			}
			$one['item'] = array();
			if (isset($i->Variations))
			{
				$one['variation'] = Q::ini('custom_flag/online_ebay_listing_variation/variation/value');
				if (is_array($i->Variations->Variation))
				{
					$variation = $i->Variations->Variation;
				}
				else
				{
					$variation = array($i->Variations->Variation);
				}
				foreach ($variation as $v)
				{
					if (isset($v->SKU))
					{
						$one_item = array(
							'sku' => $v->SKU,
							'quantity' => (isset($v->Quantity) ? $v->Quantity : 0) - $v->SellingStatus->QuantitySold,
							'sold_quantity' => $v->SellingStatus->QuantitySold,
							'start_price' => $v->StartPrice,
							'specifics' => array()
						);
						if (isset($v->VariationSpecifics))
						{
							if (is_array($v->VariationSpecifics->NameValueList))
							{
								$specifics = $v->VariationSpecifics->NameValueList;
							}
							else
							{
								$specifics = array($v->VariationSpecifics->NameValueList);
							}
							foreach ($specifics as $s)
							{
								$one_item['specifics'][] = array('name' => $s->Name, 'value' => $s->Value);
							}
						}
						$one['item'][] = $one_item;
					}
				}
			}
			else
			{
				$one['variation'] = Q::ini('custom_flag/online_ebay_listing_variation/not_variation/value');
				if (isset($i->SKU))
				{
					$one_item = array(
						'sku' => $i->SKU,
						'quantity' => $i->Quantity - $i->SellingStatus->QuantitySold,
						'sold_quantity' => $i->SellingStatus->QuantitySold,
						'start_price' => $i->StartPrice
					);
					if (isset($i->BuyItNowPrice))
					{
						$one_item['buy_it_now_price'] = $i->BuyItNowPrice;
					}
					$one['item'][] = $one_item;
				}
			}
			$listings[] = $one;
		}
		return $listings;
	}
	
	static function EbayItemDetailArray($item)
	{
		$listing = array();
		$currency = Currency::find('code=?', $item->Currency)->asArray()->getOne();
		$listing['site'] = $item->Site;
		$listing['currency_id'] = $currency['id'];
		$listing['item_id'] = $item->ItemID;
		$listing['primary_category'] = $item->PrimaryCategory->CategoryID;
		$listing['second_category'] = isset($item->SecondaryCategory->CategoryID) ? $item->SecondaryCategory->CategoryID : '';
		$listing['store_category'] = isset($item->Storefront->StoreCategoryID) ? $item->Storefront->StoreCategoryID : '';
		$listing['store_category2'] = isset($item->Storefront->StoreCategory2ID) ? $item->Storefront->StoreCategory2ID : '';
		$listing['title'] = $item->Title;
		$listing['condition_id'] = $item->ConditionID;
		$listing['condition_name'] = $item->ConditionDisplayName;
		$listing['condition_description'] = isset($item->ConditionDescription) ? $item->ConditionDescription : '';
		$listing['listing_type'] = $item->ListingType;
		$listing['listing_duration'] = $item->ListingDuration;
		$listing['start_price'] = $item->StartPrice;
		$listing['quantity'] = $item->Quantity - $item->SellingStatus->QuantitySold;
		$listing['sold_quantity'] = $item->SellingStatus->QuantitySold;
		$listing['description'] = $item->Description;
		$listing['counter'] = $item->HitCounter;
		$listing['hit_count'] = $item->HitCount;
		$listing['watch_count'] = $item->WatchCount;
		$listing['dispatch_time'] = $item->DispatchTimeMax;
		$listing['auto_pay'] = (isset($item->AutoPay) && $item->AutoPay == 'true') ? Q::ini('custom_flag/online_ebay_listing_auto_pay/auto/value') : Q::ini('custom_flag/online_ebay_listing_auto_pay/not_auto/value');
		$listing['get_it_fast'] = (isset($item->GetItFast) && $item->GetItFast == 'ture') ? Q::ini('csutom_flag/online_ebay_listing_get_it_fast/fast/value') : Q::ini('csutom_flag/online_ebay_listing_get_it_fast/not_fast/value');
		$listing['country'] = $item->Country;
		$listing['postal_code'] = isset($item->PostalCode) ? $item->PostalCode : '';
		$listing['location'] = $item->Location;
		$listing['return_policy'] = $item->ReturnPolicy->ReturnsAcceptedOption;
		$listing['return_option_restocking'] = isset($item->ReturnPolicy->RestockingFeeValueOption) ? $item->ReturnPolicy->RestockingFeeValueOption : '';
		$listing['return_option_within'] = isset($item->ReturnPolicy->ReturnsWithinOption) ? $item->ReturnPolicy->ReturnsWithinOption : '';
		$listing['return_option_refund'] = isset($item->ReturnPolicy->RefundOption) ? $item->ReturnPolicy->RefundOption : '';
		$listing['return_option_paid_by'] = isset($item->ReturnPolicy->ShippingCostPaidByOption) ? $item->ReturnPolicy->ShippingCostPaidByOption : '';
		$listing['return_option_description'] = isset($item->ReturnPolicy->Description) ? $item->ReturnPolicy->Description : '';
		$listing['sell_status'] = Helper_BSS_Normal::getIniOne('online_ebay_listing_sell_status', 'name', $item->SellingStatus->ListingStatus, 'value');
		$listing['start_time'] = Helper_BSS_Normal::timeStringToStandard($item->ListingDetails->StartTime);
		$listing['end_time'] = Helper_BSS_Normal::timeStringToStandard($item->ListingDetails->EndTime);
		$listing['view_url'] = $item->ListingDetails->ViewItemURL;
		$listing_picture = array();
		if (isset($item->PictureDetails->PictureURL))
		{
			if (is_array($item->PictureDetails->PictureURL))
			{
				$listing_picture = $item->PictureDetails->PictureURL;
			}
			else
			{
				$listing_picture = array($item->PictureDetails->PictureURL);
			}
		}
		if (isset($item->PictureDetails->GalleryURL))
		{
			$index = array_search($item->PictureDetails->GalleryURL, $listing_picture);
			if ($index !== false)
			{
				unset($listing_picture[$index]);
			}
			array_unshift($listing_picture, $item->PictureDetails->GalleryURL);
		}
		$picture_sort = 0;
		$listing['picture']['listing_picture'] = array();
		$listing['picture']['variation_picture'] = array();
		foreach ($listing_picture as $p)
		{
			$listing['picture']['listing_picture'][] = array('specific_name' => '', 'specific_value' => '', 'picture_url' => $p, 'sort' => $picture_sort++);
		}
		$listing['item_specifics'] = array();
		if (isset($item->ItemSpecifics))
		{
			if (is_array($item->ItemSpecifics->NameValueList))
			{
				$specifics = $item->ItemSpecifics->NameValueList;
			}
			else
			{
				$specifics = array($item->ItemSpecifics->NameValueList);
			}
			foreach ($specifics as $s)
			{
				$listing['item_specifics'][] = array('name' => $s->Name, 'value' => (is_array($s->Value) ? $s->Value[0] : $s->Value));
			}
		}
		$listing['shipping_service']['domestic_shipping'] = array();
		$listing['shipping_service']['international_shipping'] = array();
		if (isset($item->ShippingDetails->ShippingServiceOptions))
		{
			if (is_array($item->ShippingDetails->ShippingServiceOptions))
			{
				$domestic = $item->ShippingDetails->ShippingServiceOptions;
			}
			else
			{
				$domestic = array($item->ShippingDetails->ShippingServiceOptions);
			}
			foreach ($domestic as $d)
			{
				$listing['shipping_service']['domestic_shipping'][] = array(
					'service' => $d->ShippingService,
					'cost' => $d->ShippingServiceCost,
					'additional_cost' => isset($d->ShippingServiceCost) ? $d->ShippingServiceCost : 0,
					'free' => (isset($d->FreeShipping) && $d->FreeShipping == 'true') ? Q::ini('custom_flag/online_ebay_listing_shipping_free/free/value') : Q::ini('flag/online_ebay_listing_shipping_free/not_free/value'),
					'sort' => $d->ShippingServicePriority
				);
			}
		}
		if (isset($item->ShippingDetails->InternationalShippingServiceOption))
		{
			if (is_array($item->ShippingDetails->InternationalShippingServiceOption))
			{
				$international = $item->ShippingDetails->InternationalShippingServiceOption;
			}
			else
			{
				$international = array($item->ShippingDetails->InternationalShippingServiceOption);
			}
			foreach ($international as $i)
			{
				$shipping_location = is_array($i->ShipToLocation) ? $i->ShipToLocation : array($i->ShipToLocation);
				$listing['shipping_service']['international_shipping'][] = array(
					'service' => $i->ShippingService,
					'cost' => $i->ShippingServiceCost,
					'additional_cost' => isset($i->ShippingServiceAdditionalCost) ? $i->ShippingServiceAdditionalCost : 0,
					'free' => (isset($i->FreeShipping) && $i->FreeShipping == 'true') ? Q::ini('custom_flag/online_ebay_listing_shipping_free/free/value') : Q::ini('flag/online_ebay_listing_shipping_free/not_free/value'),
					'shipping_location' => implode(',', $shipping_location),
					'sort' => $i->ShippingServicePriority
				);
			}
		}
		$listing['item'] = array();
		if (isset($item->Variations))
		{
			$listing['variation'] = Q::ini('custom_flag/online_ebay_listing_variation/variation/value');
			if (isset($item->Variations->Variation))
			{
				if (is_array($item->Variations->Variation))
				{
					$variation_item = $item->Variations->Variation;
				}
				else
				{
					$variation_item = array($item->Variations->Variation);
				}
				foreach ($variation_item  as $vi)
				{
					if ($vi->SKU)
					{
						$one_item = array(
							'sku' => $vi->SKU,
							'quantity' => $vi->Quantity - $vi->SellingStatus->QuantitySold,
							'sold_quantity' => $vi->SellingStatus->QuantitySold,
							'start_price' => $vi->StartPrice,
							'specifics' => array()
						);
						if (isset($vi->VariationSpecifics))
						{
							if (is_array($vi->VariationSpecifics->NameValueList))
							{
								$variation_item_specifics = $vi->VariationSpecifics->NameValueList;
							}
							else
							{
								$variation_item_specifics = array($vi->VariationSpecifics->NameValueList);
							}
							foreach ($variation_item_specifics as $vis)
							{
								$one_item['specifics'][] = array('name' => $vis->Name, 'value' => $vis->Value);
							}
						}
						$listing['item'][] = $one_item;
					}
				}
			}
			if (isset($item->Variations->Pictures))
			{
				$variation_picture_specifics_name = $item->Variations->Pictures->VariationSpecificName;
				if (is_array($item->Variations->Pictures->VariationSpecificPictureSet))
				{
					$variation_specifics_picture_set = $item->Variations->Pictures->VariationSpecificPictureSet;
				}
				else
				{
					$variation_specifics_picture_set = array($item->Variations->Pictures->VariationSpecificPictureSet);
				}
				foreach ($variation_specifics_picture_set as $vsps)
				{
					if (isset($vsps->PictureURL))
					{
						if (is_array($vsps->PictureURL))
						{
							foreach ($vsps->PictureURL as $index => $url)
							{
								if ($index == 0)
								{
									$listing['picture']['variation_picture'][] = array('specific_name' => $variation_picture_specifics_name, 'specific_value' => $vsps->VariationSpecificValue, 'picture_url' => $url, 'sort' => $picture_sort++);
								}
								else
								{
									$listing['picture']['listing_picture'][] = array('specific_name' => '', 'specific_value' => '', 'picture_url' => $url, 'sort' => $picture_sort++);
								}
							}
						}
						else
						{
							$listing['picture']['variation_picture'][] = array('specific_name' => $variation_picture_specifics_name, 'specific_value' => $vsps->VariationSpecificValue, 'picture_url' => $vsps->PictureURL, 'sort' => $picture_sort++);
						}
					}
				}
			}
		}
		else
		{
			$listing['variation'] = Q::ini('custom_flag/online_ebay_listing_variation/not_variation/value');
			if (isset($item->SKU))
			{
				$one_item = array(
					'sku' => $item->SKU,
					'quantity' => $item->Quantity - $item->SellingStatus->QuantitySold,
					'sold_quantity' => $item->SellingStatus->QuantitySold,
					'start_price' => $item->StartPrice
				);
				if (isset($i->BuyItNowPrice))
				{
					$one_item['buy_it_now_price'] = $i->BuyItNowPrice;
				}
				$listing['item'][] = $one_item;
			}
		}
		return $listing;
	}
}